package com.vieewer.btdemo.view.test

import android.content.Context
import android.graphics.Rect
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.vieewer.btdemo.R
import com.vieewer.btdemo.utils.SnackbarUtil
import android.support.design.widget.Snackbar
import android.support.v4.content.ContextCompat
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import com.nightonke.boommenu.BoomButtons.BoomButton
import com.nightonke.boommenu.BoomButtons.ButtonPlaceEnum
import com.nightonke.boommenu.BoomButtons.TextInsideCircleButton
import com.nightonke.boommenu.BoomMenuButton
import com.nightonke.boommenu.ButtonEnum
import com.nightonke.boommenu.OnBoomListenerAdapter
import com.nightonke.boommenu.Piece.PiecePlaceEnum
import com.nightonke.boommenu.Util
import com.vieewer.btdemo.view.gdmap.AMapLaneInfo
import com.vieewer.btdemo.view.phonecall.BluetoothManager
import kotlinx.android.synthetic.main.activity_test.*
import android.content.Context.WIFI_SERVICE
import android.content.Intent
import android.hardware.camera2.CameraCharacteristics
import com.alibaba.fastjson.serializer.AwtCodec.support
import android.support.v4.app.SupportActivity
import android.support.v4.app.SupportActivity.ExtraData
import android.support.v4.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.net.Uri
import android.net.wifi.WifiManager
import android.os.Environment
import android.os.StrictMode
import android.provider.MediaStore
import com.lindroid.androidutilskt.extension.shortToast
import com.vieewer.btdemo.utils.PermissionUtil
import java.io.File
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.*


class TestActivity : AppCompatActivity() {

    lateinit var mInflater : LayoutInflater

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)
        PermissionUtil.requestPermissions(this)
        PermissionUtil.checkPermission(this)
        var builder = StrictMode.VmPolicy.Builder()
        StrictMode.setVmPolicy(builder.build())
        builder.detectFileUriExposure()

        initViews()

  /*      var data = intent.getSerializableExtra("laneInfoData") as AMapLaneInfo
        if (data != null) {
            driveWayLinear.buildDriveWay(data)
        }
*/

        var imagePadding = Rect(50,50,50,50)

        bmb.buttonEnum = ButtonEnum.TextInsideCircle
        bmb.piecePlaceEnum = PiecePlaceEnum.DOT_3_1
        bmb.buttonPlaceEnum = ButtonPlaceEnum.SC_3_1


        bmb.addBuilder(
            TextInsideCircleButton.Builder()
                .normalTextRes(R.string.map_switch_app)
                .normalImageRes(R.drawable.icon_map_switch_app_n)
                .normalTextColorRes(R.color.light_gray2)
                .highlightedImageRes(R.drawable.icon_map_switch_app_h)
                .highlightedTextColorRes(R.color.black)
                .normalColorRes(R.color.white)
                .imagePadding(imagePadding)

        )
        bmb.addBuilder(
            TextInsideCircleButton.Builder()
                .normalTextRes(R.string.map_exit_app)
                .normalImageRes(R.drawable.icon_map_exit_n)
                .normalTextColorRes(R.color.light_gray2)
                .highlightedImageRes(R.drawable.icon_map_exit_h)
                .highlightedTextColorRes(R.color.black)
                .normalColorRes(R.color.white)
                .imagePadding(imagePadding)
        )


        bmb.addBuilder(
            TextInsideCircleButton.Builder()
                .normalImageRes(R.drawable.icon_map_cancel_n)
                .normalTextRes(R.string.map_cancel)
                .normalTextColorRes(R.color.light_gray2)
                .highlightedImageRes(R.drawable.icon_map_cancel_h)
                .highlightedTextColorRes(R.color.black)
                .normalColorRes(R.color.white)
                .imagePadding(imagePadding)
        )

        //menu item 点击事件
        bmb.onBoomListener = object : OnBoomListenerAdapter() {
            override fun onClicked(index: Int, boomButton: BoomButton) {
                super.onClicked(index, boomButton)
                Log.i("yjj", "index >>" + index)
            }
        }


        txt_test.setOnClickListener {
            bmb.boom()
        }




  /*      mInflater = LayoutInflater.from(this)

        var snackbar = Snackbar.make(coordinatorLayout, "这是massage", Snackbar.LENGTH_LONG)
        SnackbarUtil.setSnackbarColor(snackbar, SnackbarUtil.blue)
        SnackbarUtil.SnackbarAddView(snackbar, R.layout.snackbar_addview, 0)
        snackbar.show()
        txt_test.setOnClickListener {
            BluetoothManager.getInstance().dial("15068747948", object : BluetoothManager.OnCallResultListener{
                override fun onBluetoothIsClosed() {
                    Log.i("yjj", "onBluetoothIsClosed")
                }

                override fun onDeviceIsEmpty() {
                    Log.i("yjj", "onDeviceIsEmpty")
                }

                override fun onPhoneIsInValid() {
                    Log.i("yjj", "onPhoneIsInValid")
                }

                override fun onError(errorMsg: String?) {
                    Log.i("yjj", "onError")
                }

            })

        }*/

        testAutoCamera()


    }

    private fun testAutoCamera() {
        //int[] afAvailableModes = cameraCharacteristics.get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES);
    }

    private fun initViews() {
        txt_close_wifi.setOnClickListener {
            //获取wifi管理服务
            var wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
            //获取wifi开关状态
            var status = wifiManager.wifiState
            if (status == WifiManager.WIFI_STATE_ENABLED ) {
                //wifi打开状态则关闭
                wifiManager.isWifiEnabled = false
                shortToast("wifi已断开")
            } else {
                //关闭状态则打开
                //wifiManager.setWifiEnabled(true);
                //Toast.makeText(MainActivity.this, "wifi已打开", Toast.LENGTH_SHORT).show();
            }


        }

        txt_open_camera.setOnClickListener {
            var imageFile = createImageFile()//创建用来保存照片的文件
            var intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile))
            startActivityForResult(intent, 0)
        }



    }


    /**
     * 创建用来存储图片的文件，以时间来命名就不会产生命名冲突
     * @return 创建的图片文件
     */
    private fun createImageFile(): File? {
        val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
        val imageFileName = "JPEG_" + timeStamp + "_"

        val MEDIA_DIR = Environment.getExternalStorageDirectory().path + "/DCIM/Camera"
        var directoryFile = File(MEDIA_DIR)
        //val storageDir = getExternalFilesDir(Environment.DIRECTORY_DCIM)
        var imageFile: File? = null
        try {
            imageFile = File.createTempFile(imageFileName, ".jpg", directoryFile)
        } catch (e: IOException) {
            e.printStackTrace()
        }

        return imageFile
    }

}